package leetcode.l0617;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * @author Retain
 * @date 2021/6/17 17:53
 */
public class ClazzTest {
    public static void main(String[] args) throws NoSuchMethodException, NoSuchFieldException {
        Class<User> clazz = User.class;
        Constructor<User> stringConstructor = clazz.getDeclaredConstructor(String.class);
        System.out.println("获得带String参数的有参构造器stringConstructor" + stringConstructor.getName());
        Field name = clazz.getDeclaredField("name");
        System.out.println("获得name字段："+name.getName());
        Method method = clazz.getDeclaredMethod("setN   ame", String.class);
        System.out.println("获得带String参数,方法名为setName的Method:"+method.getName());
        User.MyAnnotation annotation = clazz.getAnnotation(User.MyAnnotation.class);
        System.out.println("获得类上MyAnnotation类型的注解:"+annotation);
        Class<?>[] interfaces = clazz.getInterfaces();
        System.out.println("获得类实现的所有接口：");
        for (Class<?> anInterface : interfaces) {
            System.out.println(anInterface.getName());
        }
        //获得包对象
        Package apackage = clazz.getPackage();
        System.out.println("获得类所在的包:"+apackage);
    }


}
